package com.sqx.modules.health.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.modules.health.common.Constants;
import com.sqx.modules.health.common.Result;
import com.sqx.modules.health.entity.Glucose;
import com.sqx.modules.health.service.IGlucoseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author gxy
 * @since 2022-05-23
 */

@Slf4j
@Api(value = "", tags = "康和血糖仪设备对接", description = "")
@RestController
@RequestMapping("/health/glucose")
public class GlucoseController {
    @Autowired
    private IGlucoseService glucoseService;

    // 新增或更新
    @PostMapping("/save")
    @ApiOperation(value = "新增或更新", notes = "新增或更新")
    public Result save(@RequestBody Glucose glucose) {
        return Result.success(glucoseService.saveOrUpdate(glucose));
    }

    // 根据ID删除
    @GetMapping("/del/{id}")
    @ApiOperation(value = "根据ID删除", notes = "根据ID删除")
    public Result delete(@PathVariable Integer id) {
        return Result.success(glucoseService.removeById(id));
    }

    // 根据IDS删除
    @PostMapping("/del/batch")
    @ApiOperation(value = "根据IDS删除", notes = "根据IDS删除")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(glucoseService.removeByIds(ids));
    }

    // 查询全部
    @GetMapping("/findAll")
    @ApiOperation(value = "查询全部", notes = "查询全部")
    public Result findAll() {
        return Result.success(glucoseService.list());
    }

    // 根据ID查询一条记录
    @GetMapping("/findOne")
    @ApiOperation(value = "根据ID查询一条记录", notes = "根据ID查询一条记录")
    public Result findOne(@RequestParam Integer id) {
        return Result.success(glucoseService.getById(id));
    }

    // 分页查询
    @GetMapping("/findPage")
    @ApiOperation(value = "分页查询", notes = "分页查询")
    public Result findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        QueryWrapper<Glucose> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return Result.success(glucoseService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    /**
     * 获取时间
     *
     * @return
     */
    @ApiOperation(value = "获取当前时间")
    @GetMapping("/getNowTime")
    public String getNowTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
        log.info("获取到的时间 =====>" + sdf.format(new Date()));
        return "OK" + sdf.format(new Date());
    }

    @ApiOperation(value = "保存发送数据")
    @PostMapping("/saveGlucoseValue")
    public Result saveGlucoseValue(@RequestBody JSONObject jsonObject) {
        if (jsonObject.isEmpty()) {
            return Result.error(Constants.CODE_500, "err");
        }
        // 设置值
        Glucose glucose = new Glucose();
        BigDecimal glu = BigDecimal.valueOf(Long.valueOf(jsonObject.get("GLU").toString())).divide(BigDecimal.valueOf(10));
        glucose.setGlu(String.valueOf(glu));
        String year = "20" + jsonObject.get("DATE").toString().substring(0, 2);
        String month = jsonObject.get("DATE").toString().substring(2, 4);
        String day = jsonObject.get("DATE").toString().substring(4);
        glucose.setDate(year + "-" + month + "-" + day);
        glucose.setImei(jsonObject.get("IMEI").toString());
        String time = jsonObject.get("TIME").toString();
        if (StringUtils.isNotBlank(time)) {
            String before = time.substring(0, time.length() / 2);
            String after = time.substring(time.length() / 2, time.length());
            time = before + ":" + after;
        } else {
            time = "";
        }
        glucose.setTime(time);
        glucose.setFlag(jsonObject.get("FLAG").toString());
        boolean save = glucoseService.save(glucose);
        if (save) {
            return Result.success("OK");
        } else {
            return Result.error(Constants.CODE_500, "err");
        }
    }

}

